RS485, Modbus & NVT
api.rs485Send(msg)
- Overview
- Arguments
- Example
Sends msg to RS485 bus.
Turn on RS485 using rs485State first.
- msg (string) - Data to be sent to RS485 bus
--sends 'test' string to RS485
api.rs485Send('test')
api.rs485Setup(baudrate, parity, stopBits, dataBits)
- Overview
- Arguments
- Example
Change the configuration of RS485 interface.
- baudrate (integer) - Baudrate to use for communication (up to 921600 baud)
- parity (integer) - Parity, 0 for none, 1 for odd and 2 for even parity
- stopBits (integer) - Number of stop bits, 1 or 2 allowed
- dataBits (integer) - Number of data bits, 7 or 8 allowed
--setup RS485 interface to 9600 Baud, 8E1
api.rs485Setup(9600, 2, 1, 8)
api.rs485State(state)
- Overview
- Arguments
- Example
Turns on the RS485 circuitry.
Must be used before rs485Send or rs485Receive.
- state (integer) - New state of RS485 circuitry: 0 for off, 1 for on (fast power-up)
api.rs485State(0) --turn off RS485
api.rs485Receive(timeout)
- Overview
- Arguments
- Return
- Example
Waits timeout milliseconds for data reception from RS485 bus.
Turn on RS485 using rs485State() function first.
After the first character is received the inter-character delay is 10ms. If the inter-character delay needs to be longer the function should be called again.
- timeout (integer) - The maximum time in milliseconds to wait for RS485 device answer
- answer (string) - Data received from RS485 bus in given time
- len (integer) - Number of bytes received
--waits 1s for answer from RS485 bus
ans,len = api.rs485Receive(1000)
api.modbusCrc(msg)
- Overview
- Arguments
- Return
- Example
Calculates Modbus request checksum.
- msg (string) - Modbus request
- crc (string) - Modbus crc for request
--calculate checksum for Modbus request 110100010002
req = pack.pack('<b6', 0x11,0x01,0x00,0x01,0x00,0x02)
crc = api.modbusCrc(req) --crc = "EE9B"
api.nvtProcess(buf)
- Overview
- Arguments
- Return
- Example
Processes NVT message and either sets baudrate, datasize, parity or stop size for MBUS or MODBUS.
- buf (string) - Message to be processed
- msg (string) - Message without NVT sequence
- answer (string) - NVT answer
-- If massage received (buf) send it to RS485
ret,port,buf = api.loraSend(0,1000,data)
if buf ~= nil then
buf, nvtans = api.nvtProcess(buf)
api.rs485Send(buf)
end
api.nvtEncode(msg)
- Overview
- Arguments
- Return
- Example
Encodes message to NVT format
- msg (string) - Message to be encoded to NVT
- answer (string) - NVT message
ans,len = api.rs485Receive(50)
ans = api.nvtEncode(ans)
api.loraSend(0,1,ans)